package com.person.ntv.service.sys.impl;

import java.math.BigDecimal;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.person.common.PageParams;
import com.person.exception.ServiceException;
import com.person.ntv.dao.sys.SysDeptMapper;
import com.person.ntv.entity.sys.SysDept;
import com.person.ntv.service.base.impl.BaseServiceImpl;
import com.person.ntv.service.sys.CommonService;
import com.person.ntv.service.sys.SysDeptService;
@Service("sysDeptService")
public class SysDeptServiceImpl extends BaseServiceImpl<SysDept> implements SysDeptService{
	@Autowired
   private SysDeptMapper sysDeptMapper;
	@Autowired
   private CommonService commonService;
   @Autowired
   public void setBaseMapper(){
	   super.setBaseMapper(this.sysDeptMapper);
   }

@Override
public int deleteByPrimaryKeys(BigDecimal[] ids) throws ServiceException{
	try{
		int result=0;
		//判断部门下面是否还有员工，若存在则不允许删除
		if(sysDeptMapper.hasUserInDepts(ids)>0)
			  throw new ServiceException("1");
		result = deleteByIds(StringUtils.join(ids, ','));
	    return result;
		}catch (Exception e) {
			  if("1".equals(e.getMessage()))
	          throw new ServiceException("删除的部门下还存在用户信息，");
			  else
			  throw new ServiceException("系统错误，");
	  }
}


@Override
public Page<Map<String,Object>> getDeptPage(PageParams params)
		throws ServiceException {
	try{
		PageHelper.startPage(params.getPage(),params.getRows(),params.getOrderStr(null));
		return sysDeptMapper.selectPage(params.getParams());
		}catch (ServiceException se) {
			      se.printStackTrace();
	        	 throw new ServiceException("系统错误。");
	        } catch (Exception e) {
	        	  e.printStackTrace();
		          throw new ServiceException("数据库操作错误。");
	  } 
}

@Override
public int updateSysDept(SysDept sysDept) throws ServiceException {
    try{
	//更新子部门机构
	sysDeptMapper.updateSubDeptOrgId(sysDept);
	return updateByPrimaryKeySelective(sysDept);
    }catch(Exception e){
       e.printStackTrace();
       throw new ServiceException("系统错误！");    	
    }
}
}
